//
//  ViewController.m
//  NvSellerShow
//
//  Created by Meicam on 2017/1/5.
//  Copyright © 2017年 Meicam. All rights reserved.
//

#import "MsViewController.h"
#import "NvDraftViewController.h"
#import "NvProduceDlgViewController.h"
#import "NvFilmViewController.h"
#import "NvsStreamingContext.h"
#import "NvsAssetPackageManager.h"
#import "Utils.h"
#import "NvAssetManager.h"
#import "NvAssetModel.h"

@interface MsViewController () 

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomViewHeightConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *firstViewTopConstraint;
- (IBAction)videoFor11:(UITapGestureRecognizer *)sender;

- (IBAction)videoFor916:(UITapGestureRecognizer *)sender;
- (IBAction)videoFor169:(UITapGestureRecognizer *)sender;

- (IBAction)myVideoClick:(UITapGestureRecognizer *)sender;
- (IBAction)myDraftClick:(UITapGestureRecognizer *)sender;
@property (weak, nonatomic) IBOutlet UIImageView *merchantThumbView;
@property (weak, nonatomic) IBOutlet UILabel *merchantNameLabel;

@end

@implementation MsViewController

- (instancetype)init {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"NvSellerResource" ofType:@"bundle"];
    NSBundle *myBundle = [NSBundle bundleWithPath:path];
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MsMainPage" bundle:myBundle];
    self = [storyboard instantiateViewControllerWithIdentifier:@"mainPage"];
    if (!self)
        return nil;
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self initNavigationBarView];
    [self createVideoInfoPlist];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    //获取sdk授权
    if ([NvsStreamingContext verifySdkLicenseFile:[[Utils myBundle]pathForResource:@"439-24-c654c488ae9e83e2ea8e13f6549ba95c" ofType:@"lic"]]) {
        NSLog(@"authorised success");
    } else {
        NSLog(@"authorised faild");
    }
    self.merchantThumbView.layer.borderWidth = 2.0;
    self.merchantThumbView.layer.borderColor = [UIColor whiteColor].CGColor;
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    if (SCREEN_HEIGHT <= 568) {
        self.firstViewTopConstraint.constant = 15.0;
    } else if (SCREEN_HEIGHT <= 667 && SCREEN_HEIGHT > 568){
        self.firstViewTopConstraint.constant = 45.0;
    } else {
        self.firstViewTopConstraint.constant = 100.0;
    }
}

- (void)initNavigationBarView {
    self.view.backgroundColor = UIColorHexRGB(@"#F5F5F5");
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    [backBtn setBackgroundImage:[UIImage imageFromBundle:@"back_icon"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    self.title = @"视频制作";
    
    self.merchantThumbView.image = self.iconImage;
    self.merchantNameLabel.text = self.busnessName;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)nvCreateModeViewDoneWithMode:(NvSSVideoCreateScale)mode {
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"ProduceDlg" bundle:[Utils myBundle]];
    self.definesPresentationContext = YES;
    NvProduceDlgViewController *produceDlgView = [story instantiateViewControllerWithIdentifier:@"NvProduceDlgViewController"];
    produceDlgView.editmode = mode;
    produceDlgView.isViewIn = YES;
    [self.navigationController pushViewController:produceDlgView animated:YES];
}

- (IBAction)videoFor11:(UITapGestureRecognizer *)sender {
    [self nvCreateModeViewDoneWithMode:NvSSVideoCreateScaleSquare];
}

- (IBAction)videoFor916:(id)sender {
    [self nvCreateModeViewDoneWithMode:NvSSVideoCreateScaleVertical];
}

- (IBAction)videoFor169:(UITapGestureRecognizer *)sender {
    [self nvCreateModeViewDoneWithMode:NvSSVideoCreateScaleHorizontal];
}

- (IBAction)myDraftClick:(UITapGestureRecognizer *)sender {
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Draft" bundle:[Utils myBundle]];
    self.definesPresentationContext = YES;
    NvDraftViewController *draftViewController = [story instantiateViewControllerWithIdentifier:@"NvDraftViewController"];
    [self.navigationController pushViewController:draftViewController animated:YES];
}

- (IBAction)myVideoClick:(UITapGestureRecognizer *)sender {
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"NvFilmList" bundle:[Utils myBundle]];
    self.definesPresentationContext = YES;
    NvFilmViewController *filmViewController = [story instantiateViewControllerWithIdentifier:@"NvFilmViewController"];
    [self.navigationController pushViewController:filmViewController animated:YES];
}

- (void)doBack:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createVideoInfoPlist {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *assetPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    //得到完整的文件名
    NSString *filename=[assetPath stringByAppendingPathComponent:@"MsLocalVideoInfo.plist"];
    if (![fileManager fileExistsAtPath:filename]) {
        [fileManager createFileAtPath:filename contents:nil attributes:nil];
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
//去本地bundle资源库下去寻找已存在的文件

@end
